/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.informet.ipd.modelo.dao;

import com.informet.ipd.modelo.vo.OEstrategicoVO;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author LAPTOP01
 */
public class OEstrategicoDao extends DBase {

    Connection conn = null;

    public OEstrategicoDao() {
   super();
    }



    private void setOEstrategico(OEstrategicoVO bean, ResultSet rs) throws SQLException {
        bean.setNUM_CODOBJ_PK(rs.getInt("NUM_CODOBJ_PK"));
        bean.setNUM_IDOBJ(rs.getInt("NUM_IDOBJ"));
        bean.setTX_DESCOBJ(rs.getString("TX_DESCOBJ"));

    }


    public String spMan(int tipo,OEstrategicoVO bean) {
        String msj = "";
        try {
           
            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_OBJ(?,?,?,?)}");
            cs.setInt(1, tipo);
            cs.setInt(2, bean.getNUM_CODOBJ_PK());
            cs.setInt(3, bean.getNUM_IDOBJ());
            cs.setString(4, bean.getTX_DESCOBJ());
            cs.execute();
        } catch (Exception e) {
            msj = e.toString();
            return msj;
        }
        return msj;
    }

    public ArrayList<OEstrategicoVO> selectLista() throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<OEstrategicoVO> lista = new ArrayList<OEstrategicoVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();
            sql = "select NUM_CODOBJ_PK,TX_DESCOBJ from ME_OBJ";
            rs = stmt.executeQuery(sql);
            OEstrategicoVO bean = null;
            while (rs.next()) {
                bean = new OEstrategicoVO();
                setOEstrategico(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public List getlist() throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<OEstrategicoVO> lista = new ArrayList<OEstrategicoVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();
            sql = "select NUM_IDOBJ, NUM_CODOBJ_PK , TX_DESCOBJ as TX_DESCOBJ from ME_OBJ order by NUM_IDOBJ";
            rs = stmt.executeQuery(sql);
            OEstrategicoVO bean = null;
            while (rs.next()) {
                bean = new OEstrategicoVO();
                setOEstrategico(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }


    public int flagOEst(int id) throws SQLException {

        Statement stmt = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<OEstrategicoVO> lista = new ArrayList<OEstrategicoVO>(0);
        //lista=null;
        String sql = "";
        int flag=0;
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            sql = "select NUM_IDOBJ, NUM_CODOBJ_PK , TX_DESCOBJ as TX_DESCOBJ from ME_OBJ where NUM_IDOBJ=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
             rs = pstmt.executeQuery();
            OEstrategicoVO bean = null;
            while (rs.next()) {
                bean = new OEstrategicoVO();
                setOEstrategico(bean, rs);
                lista.add(bean);
                flag=1;
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return flag;
    }

      public OEstrategicoVO selectById(int id) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = null;
        OEstrategicoVO bean = null;
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            String sql = "select NUM_CODOBJ_PK,TX_DESCOBJ from ME_OBJ where NUM_CODOBJ_PK=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            bean = new OEstrategicoVO();
            if (rs.next()) {
                setOEstrategico(bean, rs);
            }
            rs.close();
            pstmt.close();
        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }

        return bean;
    }

}
